home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / PROSFS.C < prev    next >
C/C++ Source or Header  |  1992-05-04  |  11KB  |  352 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/prosfs.c,v 1.8 1992/05/04 21:14:49 jinx Exp $
  4.  
  5. Copyright (c) 1987-92 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Primitives to perform file-system operations. */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39. #include "osfile.h"
  40. #include "osfs.h"
  41. #include "osio.h"
  42. #ifdef DOS386
  43. #  include <sys\stat.h>
  44. #endif
  45.  
  46. extern int EXFUN (OS_channel_copy,
  47.           (off_t source_length,
  48.            Tchannel source_channel,
  49.            Tchannel destination_channel));
  50.  
  51. #define STRING_RESULT(expression)                    \
  52. {                                    \
  53.   CONST char * result = (expression);                    \
  54.   PRIMITIVE_RETURN                            \
  55.     ((result == 0)                            \
  56.      ? SHARP_F                                \
  57.      : (char_pointer_to_string ((unsigned char *) result)));        \
  58. }
  59.  
  60. DEFINE_PRIMITIVE ("FILE-EXISTS?", Prim_file_exists_p, 1, 1,
  61.   "Return #T iff FILENAME refers to an existing file.\n\
  62. Return #F if the file doesn't exist.\n\
  63. Return zero if it's a symbolic link that points to a nonexisting file.\n\
  64. Signal an error if the file's existence is indeterminate.")
  65. {
  66.   PRIMITIVE_HEADER (1);
  67.   {
  68.     enum file_existence result = (OS_file_existence_test (STRING_ARG (1)));
  69.     PRIMITIVE_RETURN
  70.       ((result == file_doesnt_exist)
  71.        ? SHARP_F
  72.        : (result == file_does_exist)
  73.        ? SHARP_T
  74.        : FIXNUM_ZERO);
  75.   }
  76. }
  77.  
  78. DEFINE_PRIMITIVE ("FILE-ACCESS", Prim_file_access, 2, 2,
  79.   "Return #T iff FILENAME exists and is accessible according to MODE.\n\
  80. MODE is an integer between 0 and 7 inclusive, bitwise encoded:\n\
  81.   4 ==> file is readable;\n\
  82.   2 ==> file is writable;\n\
  83.   1 ==> file is executable.")
  84. {
  85.   PRIMITIVE_HEADER (2);
  86.   PRIMITIVE_RETURN
  87.     (BOOLEAN_TO_OBJECT
  88.      (OS_file_access ((STRING_ARG (1)), (arg_index_integer (2, 8)))));
  89. }
  90.  
  91. DEFINE_PRIMITIVE ("FILE-DIRECTORY?", Prim_file_directory_p, 1, 1,
  92.   "Return #T iff FILENAME refers to an existing directory.\n\
  93. Otherwise #F is returned, meaning either that FILENAME doesn't exist\n\
  94.  or that it isn't a directory.")
  95. {
  96.   PRIMITIVE_HEADER (1);
  97.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (OS_file_directory_p (STRING_ARG (1))));
  98. }
  99.  
  100. DEFINE_PRIMITIVE ("FILE-SOFT-LINK?", Prim_file_soft_link_p, 1, 1,
  101.   "Iff FILENAME refers to an existing soft link, return the link contents.\n\
  102. Otherwise #F is returned, meaning either that FILENAME doesn't exist\n\
  103.  or that it isn't a soft link.")
  104. {
  105.   PRIMITIVE_HEADER (1);
  106.   STRING_RESULT (OS_file_soft_link_p (STRING_ARG (1)));
  107. }
  108.  
  109. DEFINE_PRIMITIVE ("FILE-REMOVE", Prim_file_remove, 1, 1,
  110.   "Delete file FILENAME.\n\
  111. If FILENAME is a soft link, the link is deleted.")
  112. {
  113.   PRIMITIVE_HEADER (1);
  114.   OS_file_remove (STRING_ARG (1));
  115.   PRIMITIVE_RETURN (UNSPECIFIC);
  116. }
  117.  
  118. DEFINE_PRIMITIVE ("FILE-REMOVE-LINK", Prim_file_remove_link, 1, 1,
  119.   "If file FILENAME is a link to another file (hard or soft), remove it.")
  120. {
  121.   PRIMITIVE_HEADER (1);
  122.   OS_file_remove_link (STRING_ARG (1));
  123.   PRIMITIVE_RETURN (UNSPECIFIC);
  124. }
  125.  
  126. DEFINE_PRIMITIVE ("FILE-RENAME", Prim_file_rename, 2, 2,
  127.   "Rename file FROM-NAME to TO-NAME.")
  128. {
  129.   PRIMITIVE_HEADER (2);
  130.   OS_file_rename ((STRING_ARG (1)), (STRING_ARG (2)));
  131.   PRIMITIVE_RETURN (UNSPECIFIC);
  132. }
  133.  
  134. DEFINE_PRIMITIVE ("FILE-LINK-HARD", Prim_file_link_hard, 2, 2,
  135.   "Create a hard link from file FROM-NAME to file TO-NAME.\n\
  136. TO-NAME becomes another name for the file FROM-NAME.")
  137. {
  138.   PRIMITIVE_HEADER (2);
  139.   OS_file_link_hard ((STRING_ARG (1)), (STRING_ARG (2)));
  140.   PRIMITIVE_RETURN (UNSPECIFIC);
  141. }
  142.  
  143. DEFINE_PRIMITIVE ("FILE-LINK-SOFT", Prim_file_link_soft, 2, 2,
  144.   "Create a soft link from file FROM-NAME to file TO-NAME.\n\
  145. TO-NAME becomes a soft link containing the string FROM-NAME.")
  146. {
  147.   PRIMITIVE_HEADER (2);
  148.   OS_file_link_soft ((STRING_ARG (1)), (STRING_ARG (2)));
  149.   PRIMITIVE_RETURN (UNSPECIFIC);
  150. }
  151.  
  152. DEFINE_PRIMITIVE ("LINK-FILE", Prim_link_file, 3, 3,
  153.   "This is an obsolete primitive.  Use `file-link-hard' or `file-link-soft'.\n\
  154. Create a new name for file FROM-NAME, called TO-NAME.\n\
  155. If third arg HARD? is #F, a soft link is created;\n\
  156.  otherwise a hard link is created.")
  157. {
  158.   PRIMITIVE_HEADER (3);
  159.   {
  160.     CONST char * from_name = (STRING_ARG (1));
  161.     CONST char * to_name = (STRING_ARG (2));
  162.     if ((ARG_REF (3)) != SHARP_F)
  163.       OS_file_link_hard (from_name, to_name);
  164.     else
  165.       OS_file_link_soft (from_name, to_name);
  166.   }
  167.   PRIMITIVE_RETURN (UNSPECIFIC);
  168. }
  169.  
  170. #ifndef FILE_COPY_BUFFER_LENGTH
  171. #define FILE_COPY_BUFFER_LENGTH 8192
  172. #endif
  173.  
  174. int
  175. DEFUN (OS_channel_copy, (source_length, source_channel, destination_channel),
  176.        off_t source_length AND
  177.        Tchannel source_channel AND
  178.        Tchannel destination_channel)
  179. {
  180.   char buffer [FILE_COPY_BUFFER_LENGTH];
  181.   off_t transfer_length =
  182.     ((source_length > (sizeof (buffer))) ? (sizeof (buffer)) : source_length);
  183.  
  184.   while (source_length > 0)
  185.   {
  186.     long nread =
  187.       (OS_channel_read (source_channel, buffer, transfer_length));
  188.     if (nread <= 0)
  189.     {
  190.       return (-1);
  191.     }
  192.     if ((OS_channel_write (destination_channel, buffer, nread)) <
  193.     nread)
  194.     {
  195.       return (-1);
  196.     }
  197.     source_length -= nread;
  198.     if (source_length < (sizeof (buffer)))
  199.       transfer_length = source_length;
  200.   }
  201.   return (0);
  202. }  
  203.  
  204. void
  205. DEFUN (OS_file_copy, (from_name, to_name),
  206.        CONST char * from_name AND
  207.        CONST char * to_name)
  208. {
  209.   int result;
  210.   Tchannel source_channel = (OS_open_input_file (from_name));
  211.   Tchannel destination_channel = (OS_open_output_file (to_name));
  212.   off_t source_length = (OS_file_length (source_channel));
  213.  
  214.   result = (OS_channel_copy (source_length,
  215.                  source_channel,
  216.                  destination_channel));
  217.   
  218.   OS_channel_close (source_channel);
  219.   OS_channel_close (destination_channel);
  220.  
  221.   if (result < 0)
  222.   {
  223.     signal_error_from_primitive (ERR_IO_ERROR);
  224.   }
  225.   return;
  226. }
  227.  
  228. DEFINE_PRIMITIVE ("FILE-COPY", Prim_file_copy, 2, 2,
  229.   "Make a new copy of the file FROM-NAME, called TO-NAME.")
  230. {
  231.   PRIMITIVE_HEADER (2);
  232.   OS_file_copy ((STRING_ARG (1)), (STRING_ARG (2)));
  233.   PRIMITIVE_RETURN (UNSPECIFIC);
  234. }
  235.  
  236. DEFINE_PRIMITIVE ("DIRECTORY-MAKE", Prim_directory_make, 1, 1,
  237.   "Create a new directory, called NAME.")
  238. {
  239.   PRIMITIVE_HEADER (1);
  240.   OS_directory_make (STRING_ARG (1));
  241.   PRIMITIVE_RETURN (UNSPECIFIC);
  242. }
  243.  
  244. DEFINE_PRIMITIVE ("DIRECTORY-OPEN-NOREAD", Prim_directory_open_noread, 1, 1,
  245.   "Open the directory NAME for reading.")
  246. {
  247.   PRIMITIVE_HEADER (1);
  248.   if (OS_directory_index >= 0)
  249.     error_external_return ();
  250.   OS_directory_index = (OS_directory_open (STRING_ARG (1)));
  251.   PRIMITIVE_RETURN (UNSPECIFIC);
  252. }
  253.  
  254. DEFINE_PRIMITIVE ("DIRECTORY-CLOSE", Prim_directory_close, 0, 0,
  255.   "Close the directory opened by `directory-open'.")
  256. {
  257.   PRIMITIVE_HEADER (0);
  258.   if (OS_directory_index >= 0)
  259.     {
  260.       OS_directory_close (OS_directory_index);
  261.       OS_directory_index = (-1);
  262.     }
  263.   PRIMITIVE_RETURN (UNSPECIFIC);
  264. }
  265.  
  266. #define DIRREAD(expr)                            \
  267. {                                    \
  268.   CONST char * result = (expr);                        \
  269.   if (result == 0)                            \
  270.     {                                    \
  271.       OS_directory_close (OS_directory_index);                \
  272.       OS_directory_index = (-1);                    \
  273.       PRIMITIVE_RETURN (SHARP_F);                    \
  274.     }                                    \
  275.   PRIMITIVE_RETURN                            \
  276.     (char_pointer_to_string ((unsigned char *) result));        \
  277. }
  278.  
  279. DEFINE_PRIMITIVE ("DIRECTORY-OPEN", Prim_directory_open, 1, 1,
  280.   "Open the directory NAME for reading.\n\
  281. If successful, return the first filename in the directory as a string.\n\
  282. If there is no such file, #F is returned.")
  283. {
  284.   PRIMITIVE_HEADER (1);
  285.   if (OS_directory_index >= 0)
  286.     error_external_return ();
  287.   OS_directory_index = (OS_directory_open (STRING_ARG (1)));
  288.   DIRREAD (OS_directory_read (OS_directory_index));
  289. }
  290.  
  291. DEFINE_PRIMITIVE ("DIRECTORY-READ", Prim_directory_read, 0, 0,
  292.   "Read and return a filename from the directory opened by `directory-open'.\n\
  293. Return #F if there are no more files in the directory.")
  294. {
  295.   PRIMITIVE_HEADER (0);
  296.   if (OS_directory_index < 0)
  297.     error_external_return ();
  298.   DIRREAD (OS_directory_read (OS_directory_index));
  299. }
  300.  
  301. DEFINE_PRIMITIVE ("DIRECTORY-READ-MATCHING", Prim_directory_read_matching, 1, 1,
  302.   "Read and return a filename from the directory opened by `directory-open'.\n\
  303. The filename must begin with the argument string.\n\
  304. Return #F if there are no more matching files in the directory.")
  305. {
  306.   PRIMITIVE_HEADER (1);
  307.   if (OS_directory_index < 0)
  308.     error_external_return ();
  309.   DIRREAD (OS_directory_read_matching (OS_directory_index, (STRING_ARG (1))));
  310. }
  311.  
  312. DEFINE_PRIMITIVE ("NEW-DIRECTORY-OPEN", Prim_new_directory_open, 1, 1,
  313.   "Open the directory NAME for reading, returning a directory number.")
  314. {
  315.   PRIMITIVE_HEADER (1);
  316.   PRIMITIVE_RETURN (long_to_integer (OS_directory_open (STRING_ARG (1))));
  317. }
  318.  
  319. static unsigned int
  320. DEFUN (arg_directory_index, (argument), unsigned int argument)
  321. {
  322.   long index = (arg_integer (argument));
  323.   if (! (OS_directory_valid_p (index)))
  324.     error_bad_range_arg (argument);
  325.   return (index);
  326. }
  327.  
  328. DEFINE_PRIMITIVE ("NEW-DIRECTORY-CLOSE", Prim_new_directory_close, 1, 1,
  329.   "Close DIRECTORY.")
  330. {
  331.   PRIMITIVE_HEADER (1);
  332.   OS_directory_close (arg_directory_index (1));
  333.   PRIMITIVE_RETURN (UNSPECIFIC);
  334. }
  335.  
  336. DEFINE_PRIMITIVE ("NEW-DIRECTORY-READ", Prim_new_directory_read, 1, 1,
  337.   "Read and return a filename from DIRECTORY, or #F if no more files.")
  338. {
  339.   PRIMITIVE_HEADER (1);
  340.   STRING_RESULT (OS_directory_read (arg_directory_index (1)));
  341. }
  342.  
  343. DEFINE_PRIMITIVE ("NEW-DIRECTORY-READ-MATCHING", Prim_new_directory_read_match, 2, 2,
  344.   "Read and return a filename from DIRECTORY.\n\
  345. The filename must begin with the STRING.\n\
  346. Return #F if there are no more matching files in the directory.")
  347. {
  348.   PRIMITIVE_HEADER (2);
  349.   STRING_RESULT
  350.     (OS_directory_read_matching ((arg_directory_index (1)), (STRING_ARG (2))));
  351. }
  352.